package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorRes;
import androidx.annotation.RequiresApi;

import com.sgb.kjwl.R;
import com.sgb.kjwl.interfaces.PopResult;
import com.sgb.kjwl.model.entity.PickSelectEntity2;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.util.MToast;

import java.util.List;


public class ScreenTextViewForThreeLevel<D extends PickSelectEntity2> extends RelativeLayout {

    private Context mContext;
    private LinearLayout mLlContent;
    private TextView mTvContent;
    private ImageView img;
    private ThreeLevelPopWindow<D> mThreeLevelPopWindow;
    private String mName;
    private int mType;
    private boolean mIsSimple = false;
    private PopResult mPopResult;


    public void setmPopResult(PopResult mPopResult) {
        this.mPopResult = mPopResult;
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public ScreenTextViewForThreeLevel(Context context) {
        this(context, null);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public ScreenTextViewForThreeLevel(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    public ScreenTextViewForThreeLevel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init(attrs, context);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    private void init(AttributeSet attrs, Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_item_screen_text_for_circle, this);
        mLlContent = view.findViewById(R.id.ll_content);
        mTvContent = view.findViewById(R.id.tv_content);
        img = view.findViewById(R.id.img);

        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ScreenTextViewYoung, 0, 0);

            try {
                String content = typedArray.getString(R.styleable.ScreenTextViewYoung_textContent);
                if (content != null) {
                    mTvContent.setText(content);
                } else {
                    mTvContent.setText("");
                }

                int color = typedArray.getColor(R.styleable.ScreenTextViewYoung_textColor, getResources().getColor(R.color.color_333333));
                mTvContent.setTextColor(color);

                int background = typedArray.getColor(R.styleable.ScreenTextViewYoung_backgroundColor, 0);

                mThreeLevelPopWindow = new ThreeLevelPopWindow<>(context);

                mThreeLevelPopWindow.setBackground(background);

                mType = typedArray.getInt(R.styleable.ScreenTextViewYoung_typeForPop, 0);
                if (mType == 0) {
                    mLlContent.setBackgroundResource(R.color.white);
                } else {
                    mLlContent.setBackgroundResource(R.drawable.search_bg);
                }

                mIsSimple = typedArray.getBoolean(R.styleable.ScreenTextViewYoung_simpleText,false);
                isSimple(mIsSimple);

                int mBackgroundView = typedArray.getResourceId(R.styleable.ScreenTextViewYoung_backgroundView, 0);
                View bgView = inflate(context, mBackgroundView,this);
                mThreeLevelPopWindow.setBgView(bgView);

                @ColorRes  int mBackgroundColor = typedArray.getResourceId(R.styleable.ScreenTextViewYoung_backgroundColor, 0);
                mThreeLevelPopWindow.setBackground(context.getColor(mBackgroundColor));

                String provinceDefault = typedArray.getString(R.styleable.ScreenTextViewYoung_provinceDefaultText);
                String cityDefault = typedArray.getString(R.styleable.ScreenTextViewYoung_cityDefaultText);
                String areaDefault = typedArray.getString(R.styleable.ScreenTextViewYoung_areaDefaultText);
                mThreeLevelPopWindow.setDefaultText(provinceDefault == null ? "" : provinceDefault,cityDefault == null ? "" : cityDefault,areaDefault == null ? "" : areaDefault);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                typedArray.recycle();
            }
        }

        this.setOnClickListener(v -> {
            if (mThreeLevelPopWindow.getData() != null && mThreeLevelPopWindow.getData().size() != 0) {
                contentSelect(false, true);
                mThreeLevelPopWindow.showPopwindow(this, 0, 0);
            } else {
                MToast.showToast(context, "请等待数据加载完成");
            }
        });

        mThreeLevelPopWindow.setOnResultClick((key1, key2, key3) -> {
            if (mPopResult == null){
                return;
            }

            if (key2 == null && key3 == null) {
                if (mIsSimple) {
                    mName = ((AddressEntity) key1).getName();
                } else {
                    mName = ((AddressEntity) key1).getSimpleName();
                }

                mPopResult.result(mName, 0, key1);
            } else if (key3 == null) {
                if (mIsSimple) {
                    mName = ((AddressEntity) key2).getName();
                } else {
                    mName = ((AddressEntity) key2).getSimpleName();
                }
                mPopResult.result(mName, 0, key1, key2);
            } else {
                if (mIsSimple) {
                    mName = (((List<D>) key3).get(0)).getName();
                } else {
                    mName = (((List<D>) key3).get(0)).getSimpleName();
                }
                mPopResult.result(mName, 0, key1, key2, key3);
            }

        });

        mThreeLevelPopWindow.setOnDisMissClick(() -> {
            if (!mName.equals("不限")) {
                contentSelect(false, false);
            } else {
                contentSelect(true, false);
            }
        });
    }

    public void contentSelect(boolean haveContent, boolean isOpen) {
        if (!haveContent && isOpen) {
            //点击没选择 打开了弹窗
            img.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.pick_select_up_normal));
            mTvContent.setTextColor(mContext.getResources().getColor(R.color.color_5792FD));
        } else if (haveContent && isOpen) {
            //点击已选择的按钮
            img.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.pick_select_up_normal));
            mTvContent.setTextColor(mContext.getResources().getColor(R.color.color_5792FD));
            if (mType == 0) {
                return;
            }
            mLlContent.setBackground(mContext.getResources().getDrawable(R.drawable.search_new_blue_bg));
        } else if (haveContent) {
            //点击 选择或重新选择 关闭弹窗
            img.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.icon_down_red));
            if (mType == 0) {
                return;
            }
            mLlContent.setBackground(mContext.getResources().getDrawable(R.drawable.search_new_blue_bg));
        } else {
            //取消选择 选择不限或全国
            img.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.ic_down_arrow_n));
            mTvContent.setTextColor(mContext.getResources().getColor(R.color.color_333333));
            if (mType == 0) {
                return;
            }
            mLlContent.setBackground(mContext.getResources().getDrawable(R.drawable.search_bg));
        }
    }

    public void setPopWindowData(List<AddressEntity<List<AddressEntity<List<D>>>>> list) {
        mThreeLevelPopWindow.setData(list);
    }

    public void isSimple(boolean isSimple) {
        this.mIsSimple = isSimple;
    }
}